package com.base.cn.platform.os.controller.qa.record;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.log.aop.AdminModuleEnum;
import com.base.cn.platform.os.common.log.aop.OpLog;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.course.qa.record.QaTeacherCommissionService;
import com.base.cn.platform.os.service.manage.teacher.SysTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 问答讲师分成Controller
 *
 * @author LIZHIPENG
 * @since 2018/5/3
 */
@Controller
@RequestMapping("/admin/qa/record")
public class AdminTeacherCommissionController extends AdminBaseController {

    @Autowired
    private QaTeacherCommissionService qaTeacherCommissionService;
    @Autowired
    private SysTeacherService    sysTeacherService;

    /**
     * 查询数量
     * @param params 条件对象
     * @return 数量
     */
    @RequestMapping("/getCommissionCount")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "提现旁问答讲师分成")
    public Map<String,Object> getCommissionCount(HttpServletRequest request,
                                                 @RequestBody Map<String,Object> params){
        BigDecimal teacherId = loginUtil.getLoginUserTeacherId(request);
        if(BigDecimal.ZERO.compareTo(teacherId)==0){
            teacherId = new BigDecimal(-1);
        }
        params.put("teacherId",teacherId);
        params.put("status",1);
        BigDecimal count = qaTeacherCommissionService.getCommissionCount(params);
        if(count==null || count.compareTo(BigDecimal.ZERO)==0){
            return ResultUtil.ERROR("当前登录账号没有可提现的分成记录");
        }
        return ResultUtil.SUCCESS(count);
    }

    /**
     * 初始化问答讲师分成列表页面
     * @return
     */
    @RequestMapping("/queryTeacherCommissionList")
    public String queryTeacherCommissionList(Model model,@RequestParam(value = "type", required = false, defaultValue = "2")int type,@RequestParam(value = "teacherId", required = false)BigDecimal teacherId){
        model.addAttribute("type",type);
        model.addAttribute("teacherId",teacherId);
        return resultPage("/qa/record/qa-teacher-commission-list");
    }

    /**
     * ajax加载问答讲师分成列表数据
     * @param model
     * @param currentPage
     * @param params
     * @return
     */
    @RequestMapping("/ajaxQueryTeacherCommissionList")
    public String ajaxQueryTeacherCommissionList(HttpServletRequest request,
                                                 Model model,
                                                 @RequestParam(value = "currentPage", required = false, defaultValue = "1")int currentPage,
                                                 @RequestBody Map<String, Object> params){
        Map<String, Object> dataMap =null;
        Integer type =  Integer.valueOf(params.get("type").toString());
        params.remove("type");
        if(type==2){
            Map<String, Object> sysTeacher = new HashMap<>();
            sysTeacher.put("isQuestion",1);
            sysTeacher.put("teacherName",params.get("teacherName"));
            dataMap =   sysTeacherService.findSysTeacherCommissionListPage(sysTeacher, currentPage);
        }else{
            dataMap =  qaTeacherCommissionService.queryQaTeacherCommissionListPage(currentPage, params, true, true, true);

        }
        model.addAttribute("type", type);
        model.addAttribute("dataMap", dataMap);
        return resultPage("/qa/record/ajax-qa-teacher-commission-list");
    }
}
